package com.example.database;

import com.example.database.util.CsvFileReader;
import com.example.database.util.MySqlConnector;
import com.example.database.util.SqlFileExecutor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.sql.Connection;

@SpringBootApplication
public class DatabaseApplication {

    public static void main(String[] args) {

        SqlFileExecutor sqlFileExecutor = new SqlFileExecutor();
        CsvFileReader csvFileReader = new CsvFileReader();

        try
        {
            Connection conn = new MySqlConnector().getConnection();

            sqlFileExecutor.executeSql(conn,"/sql/Campus.sql");
            sqlFileExecutor.executeSql(conn,"/sql/Department.sql");
            sqlFileExecutor.executeSql(conn,"/sql/Class.sql");
            sqlFileExecutor.executeSql(conn,"/sql/Student.sql");
            sqlFileExecutor.executeSql(conn,"/sql/DeptInstructor.sql");
            sqlFileExecutor.executeSql(conn,"/sql/ClassInstructor.sql");

            sqlFileExecutor.executeSql(conn,"/sql/HealthForm.sql");
            sqlFileExecutor.executeSql(conn,"/sql/LeavingForm.sql");
            sqlFileExecutor.executeSql(conn,"/sql/InOutLog.sql");
            sqlFileExecutor.executeSql(conn,"/sql/AdmissionForm.sql");

            csvFileReader.insertIntoTableFromCSVFile(conn, "campus", "/data/Campus.csv", "UTF8", true);
            csvFileReader.insertIntoTableFromCSVFile(conn, "department", "/data/Department.csv", "UTF8", true);
            csvFileReader.insertIntoTableFromCSVFile(conn, "class", "/data/Class.csv", "UTF8", true);
            csvFileReader.insertIntoTableFromCSVFile(conn, "student", "/data/Student.csv", "UTF8", true);
            csvFileReader.insertIntoTableFromCSVFile(conn, "dept_instructor", "/data/DeptInstructor.csv", "UTF8", true);
            csvFileReader.insertIntoTableFromCSVFile(conn, "class_instructor", "/data/ClassInstructor.csv", "UTF8", true);

            csvFileReader.insertIntoTableFromCSVFile(conn, "in_out_log", "/data/InOutLog.csv", "UTF8", true);

            conn.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return;
        }

        SpringApplication.run(DatabaseApplication.class, args);
    }

}
